package com.bit.inmap;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class QandActivity extends Activity implements OnItemClickListener {
	private MyProgressDialog progressDialog;
	ArrayList<item_list> arrData = new ArrayList<item_list>();
	ListAdapter adapter;
	ListView lv;
	item_list il;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_qand);
		progressDialog = MyProgressDialog.show(this,"","",true,true,null);

		String url = "http://192.168.0.202/Qand.html";				
		DownThread thread = new DownThread (url, mResHandler);	
		thread.start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.qand, menu);
		return true;
	}
	
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
			Intent next = new Intent(QandActivity.this, QandSubActivity.class);
			next.putExtra("title", arrData.get(position).content);
			next.putExtra("value", arrData.get(position).id);
			startActivity(next);
			this.finish();
	}
	
	class DownThread extends Thread {
		String mAddr;
		ResponseHandler<String> ResHandler;
		
		DownThread(String addr, ResponseHandler<String> resHandler) { 
			mAddr = addr;
			ResHandler = resHandler;
		}
		
		public void run() {
			HttpGet get = new HttpGet(mAddr);
			DefaultHttpClient client = new DefaultHttpClient();
			
			try {
				client.execute(get, mResHandler);
			} catch (Exception e) {;}
		}
	}
	
	ResponseHandler<String> mResHandler = new ResponseHandler<String>() {
		@Override
		public String handleResponse(HttpResponse response) {
			StringBuilder html = new StringBuilder();
			
			try {
				BufferedReader br = new BufferedReader
						(new InputStreamReader(response.getEntity().getContent()));
								
				for (;;) {
					String line = br.readLine();
					if (line==null) break;
					html.append(line+'\n');
				}
				br.close();
				
				Log.d("testjson", html.toString());
				Message message = mAfterDown.obtainMessage();
				message.obj = html.toString();
				mAfterDown.sendMessage(message);
			} catch (Exception e) {;}
			return html.toString();
		}
	};
	
	Handler mAfterDown = new Handler() {
		public void handleMessage(Message msg) {
			
		//	StringBuilder sb = new StringBuilder();			
			String json = (String)msg.obj;
			
			try {
				JSONArray ja = new JSONArray(json);
				for (int i=0 ; i<ja.length() ; i++) {
					JSONObject jo = ja.getJSONObject(i);
					String title = jo.getString("title");
					String content = jo.getString("content");
					arrData.add(new item_list(title, content));
				}
				noticeList();
			} catch (JSONException e) {
				e.printStackTrace();
			}						
		}
	};
	
	private void noticeList() {
		lv = (ListView)this.findViewById(R.id.list_view);
		adapter = new ListAdapter (this, R.layout.list_item, arrData);		
		lv.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		lv.setOnItemClickListener(this);
		
		if (progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}
}
